#ifndef REQUESTITEMS_H
#define REQUESTITEMS_H

#include <QGraphicsItem>
#include <QPainter>
#include <QString>

class RequestFieldItem : public QGraphicsItem
{
public:
    explicit RequestFieldItem(QGraphicsItem * parent = 0)
        : QGraphicsItem(parent)
    {
        setEnclosed(true);
        setDotted(false);
    }
    inline QString text() const { return _text; }
    inline QString data() const { return _data; }
    inline qreal width() { return _width; }
    inline void setText( const QString& string ) { _text = string; }
    inline void setData( const QString& data ) { _data = data; }
    inline void setWidth( const qreal width ) { _width = width; }
    inline void setEnclosed(bool set) { _enclosed = set; }
    inline bool isEnclosed() { return _enclosed; }
    inline void setDotted(bool set) { _dotted = set; }
    inline bool isDotted() { return _dotted; }

    QRectF boundingRect() const
    {
        qreal penWidth = 1;
        return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                           20 + penWidth, 20 + penWidth);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
               QWidget *widget)
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);
        //painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
        if(isEnclosed()){
            painter->drawLine(0,0,0,10);
            painter->drawLine(_width+10,0,_width+10,10);
        }
        //data
        if(!_data.isEmpty()){
            QRectF r0(0, -10, _width, 20);
            QFont f(painter->font());
            f.setPointSize(f.pointSize()+1);
            f.setBold(false);
            painter->setFont(f);
            painter->drawText(r0, _data, QTextOption(Qt::AlignHCenter));

        }
        //text
        if(!_text.isEmpty()){
            QRectF r1(5, 7, _width, 16);
            //painter->save();
            QFont f(painter->font());
            f.setPointSize(6);
            f.setBold(false);
            painter->setFont(f);
            //painter->setPen(Qt::darkGray);
            painter->drawText(r1,_text,QTextOption(Qt::AlignHCenter));
        }
        QPen pen(painter->pen());
        if(isDotted()){
            pen.setStyle(Qt::DotLine);
        }
        painter->setPen(pen);
        painter->drawLine(5,5,_width+5,5);
    }

private:
    QString _text;
    QString _data;
    qreal _width;
    bool _enclosed;
    bool _dotted;
};

class RequestCellItem : public QGraphicsItem
{
public:
    explicit RequestCellItem(QGraphicsItem * parent = 0)
        : QGraphicsItem(parent)
    {
        setNum(0);
        setText(QString::null);
        setTextFontSize(5);
    }

    inline QString text() const { return _text; }
    inline qreal width() { return _width; }
    inline qreal hight() { return _hight; }
    inline void setText( const QString& string ) { _text = string; }
    inline void setWidth( const qreal width ) { _width = width; }
    inline void setHight( const qreal hight ) { _hight = hight; }
    inline void setNum( const int num ) { _num = num; }
    inline int getNum() const { return _num; }
    inline int textFontSize() { return tf_size; }
    inline void setTextFontSize(const int size) { tf_size = size; }

    QRectF boundingRect() const
    {
        qreal penWidth = 1;
        return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                           20 + penWidth, 20 + penWidth);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
               QWidget *widget)
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);
        //painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
        painter->drawRect(0,0,_width,_hight);

        //text
        QRectF r1(0, 0, _width-2, _hight-2);
        QFont f(painter->font());
        f.setPointSize(tf_size);
        painter->setFont(f);
        if(!_text.isEmpty()){
            QTextOption text_option(Qt::AlignHCenter | Qt::AlignVCenter);
            text_option.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
            painter->drawText(r1,_text,text_option);
        }
        if(_num){
            QRectF r2(2, 0, _width, _hight);
            painter->drawText(r2,QString::number(_num),QTextOption(Qt::AlignBottom));
        }
    }

private:
    QString _text;
    qreal _width;
    qreal _hight;
    int _num;
    int tf_size;
};

class RequestTableRow : public QGraphicsItem
{
public:
    explicit RequestTableRow(QGraphicsItem * parent = 0)
        : QGraphicsItem(parent)
    {
        setNumFields(15);
        setTextFontSize(8);
        setFinal(false);
    }
    inline int numFields() const { return num_fields; }
    inline void setNumFields(const int num) { num_fields = num; }
    qreal fieldWidth(const int field) { return fields_width.value(field); }
    void setFieldWidth(const int field, qreal width) { fields_width[field] = width; }
    void setFieldsWidth(const QMap<int, qreal> map) { fields_width = map; }
    QString fieldText(const int field){ return fields_text.value(field); }
    void setFieldText(const int field, const QString& text) { fields_text[field] = text; }

    inline QString itemName() const { return _name; }
    inline void setItemName( const QString& string ) { _name = string; }
    inline QString itemAmountWord() const { return _amount_word; }
    inline void setItemAmountWord( const QString& string ) { _amount_word = string; }
    inline qreal hight() { return _hight; }
    inline void setHight( const qreal hight ) { _hight = hight; }
    inline int textFontSize() { return tf_size; }
    inline void setTextFontSize(const int size) { tf_size = size; }
    inline bool isFinal() const { return final; }
    inline void setFinal(bool f) { final = f; }

    QRectF boundingRect() const
    {
        qreal penWidth = 1;
        return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                           20 + penWidth, 20 + penWidth);
    }

    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
               QWidget *widget)
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);
        qreal dx = 0;
        QFont f(painter->font());
        f.setPointSize(tf_size);
        f.setBold(false);
        QFontMetrics fm(f);
        painter->setFont(f);
        for(int i=0; i< numFields(); i++){
            qreal w = fields_width.value(i);
            painter->drawRect(dx,0,w,_hight);
            //text
            QString text = fields_text.value(i);
                if(!text.isEmpty()){
                int pixelsWide = fm.width(text);
                int pixelsHigh = fm.height();
                QRectF r1(dx, 0, pixelsWide<w?w:pixelsWide, pixelsHigh);
                QTextOption text_option(Qt::AlignHCenter);
                text_option.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
                painter->drawText(r1,text,text_option);
            }
            dx+=w;
        }
        if(!isFinal()){
            painter->drawRect(0,_hight,dx,_hight);
            QTextOption text_option(Qt::AlignLeft);
            QRectF r_i(2, _hight, dx/2, _hight);
            painter->drawText(r_i,itemName(),text_option);
            QRectF r_a(dx/2, _hight, dx/2, _hight);
            painter->drawText(r_a,itemAmountWord(),text_option);

            QPen pen;
            pen.setWidth(1);
            //bold rect #1
            qreal b_w = fields_width.value(0)+fields_width.value(1)+fields_width.value(2)+fields_width.value(3);
            painter->setPen(pen);
            painter->drawRect(0,0,b_w,_hight);
            //bold rect #2
            b_w += fields_width.value(4)+fields_width.value(5)+fields_width.value(6);
            qreal b_w2 = fields_width.value(7)+fields_width.value(8)+fields_width.value(9)+fields_width.value(10);
            painter->drawRect(b_w,0,b_w2,_hight);
            //bold rect #3
            b_w += b_w2+fields_width.value(11)+fields_width.value(12);
            b_w2 = fields_width.value(13)+fields_width.value(14);
            painter->drawRect(b_w,0,b_w2,_hight);
        }
    }

private:
    QMap<int, qreal> fields_width;
    QMap<int, QString> fields_text;
    QString _name;
    QString _amount_word;
    qreal _hight;
    int num_fields;
    int tf_size;
    bool final;

};

#endif // REQUESTITEMS_H
